package com.woniuxy.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.PageDto;
import com.woniuxy.entity.ProductLine;
import com.woniuxy.mapper.ProductLineMapper;
import com.woniuxy.service.ProductLineService;

@Service
public class ProductLineServiceImpl implements ProductLineService{
	@Autowired
	private ProductLineMapper productLineMapper;

	@Override
	public PageDto<ProductLine> findAllProductLine(int page) {
		PageDto<ProductLine> pageDto;
		synchronized (ProductLineServiceImpl.class) { //加锁
			PageHelper.startPage(page, 10);
			List<ProductLine> productLines = productLineMapper.findAllProductLine();
			PageInfo<ProductLine> pageInfo = new PageInfo<>(productLines);
			pageDto = new PageDto<ProductLine>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(productLines);
		}		
		return pageDto;	
	}

	@Override
	public int addProductLine(ProductLine productLine) {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String resDate = dateFormat.format(date);
		productLine.setTime(resDate);
		return productLineMapper.addProductLine(productLine);
	}

	@Override
	public PageDto<ProductLine> findProductLineByStatus(int status, int page) {
		PageDto<ProductLine> pageDto;
		synchronized (ProductLineServiceImpl.class) {
			PageHelper.startPage(page, 10);
			List<ProductLine> productLines = productLineMapper.findProductLineByStatus(status);
			PageInfo<ProductLine> pageInfo = new PageInfo<>(productLines);
			pageDto = new PageDto<ProductLine>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(productLines);
		}
		return pageDto;
	}

	@Override
	public PageDto<ProductLine> findProductLineByLname(String lname, int page) {
		PageDto<ProductLine> pageDto;
		synchronized (ProductLineServiceImpl.class) {
			PageHelper.startPage(page, 10);
			List<ProductLine> productLines = productLineMapper.findProductLineByLname(lname);
			PageInfo<ProductLine> pageInfo = new PageInfo<>(productLines);
			pageDto = new PageDto<ProductLine>()
						.setCurrentPage(page)
						.setPageSize(pageInfo.getPageSize())
						.setTotal(pageInfo.getPageSize())
						.setTotalPages(pageInfo.getPages())
						.setData(productLines);
		}
		return pageDto;
	}

	@Override
	public int deleteProductLineByLid(int lid) {
		return productLineMapper.delectProductLineByLid(lid);
	}

	@Override
	public int updateSwitch(int lid, int value) {
		int data;
		synchronized(ProductLineServiceImpl.class) {
			ProductLine productLine = new ProductLine().setLid(lid).setStatus(value);
			data = productLineMapper.updateSwitch(productLine);
		}
		return data;
	}

	
}
